package com.tarena.csmall.rocketmq.demo.controller;

import com.tarena.csmall.rocketmq.demo.consumer.MyTransactionConsumerListener;
import com.tarena.csmall.rocketmq.demo.service.HelloService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author java@tedu.cn
 * @version 1.0
 */
@RestController
public class HelloController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @GetMapping("/hello")
    public String sayHi(String name){
        //发送消息 Message是rocketMQTemplate支持发送的消息参数
        //和底层api方法的Message不是同一个类,相当于将底层Message包装了一次.
        Message message=
                //payLoad在和,就是body
                MessageBuilder.withPayload(name)
                        .setHeader("age",18)
                        .build();
        SendResult sendResult = rocketMQTemplate.syncSend("rocket-topic-a:tagA", message);
        //rocketMQTemplate.receive();
        //发送消息
        if (sendResult.getSendStatus().toString().equals("SEND_OK")){
            return "success";
        }else {
            return sendResult.getSendStatus().toString();
        }
    }
    /**
     * 发送半消息测试
     */
    @GetMapping("/half")
    public String sendHalf(){
        Message message=MessageBuilder.withPayload("半消息").build();
        rocketMQTemplate
                .sendMessageInTransaction(
                        "trans-test-topic",
                        message,"业务数据");
        return "success";
    }





}
